const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const facs = require('./routes/facs');
const usersRouter = require('./routes/users');
const parsing = require('./routes/parsing');
const update = require('./routes/update');
const delet = require('./routes/delet');

const app = express();

// 处理跨域
app.all('*', (req, res, next)=> {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  if (req.method === 'OPTIONS') {res.send(200); }else {next();}
})
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use( bodyParser.urlencoded({
  extended: false
}))

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(express.static(__dirname+'/public/Fiction-software-front-end-project/dist_phoneShuo'));
app.use('/', facs);

app.use('/dome', indexRouter);
app.use('/users', usersRouter);
app.use('/d', parsing);
app.use('/update',update)
app.use('/det',delet)
// error handler
app.use(function(err, req, res, next){
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
